루비에서의 신뢰성은 우연한 성공이 아니다. 그것은 구조화된 윤리 ‘ sớm 테스트, 자주 테스트’라는 철학에 기반한 것이다. 기능 코드와 함께 단위 테스트를 작성함으로써 디버깅을 지겨운 고고학적 탐사 정밀하고 실시간적인 논리 검증으로 바꾼다.
1. 단위 테스트의 철학
다음과 같은 Test::Unit 프레임워크를 사용해 로직을 Test::Unit::TestCase클래스로 감싸게 된다. 'test_' 접두사를 붙인 메서드는 test_ 개별 코드 단위를 독립적으로 조작하고 검증하는 실험실 역할을 한다.
2. 검증의 원리
검증은 코드의 논리적 장벽이다. assert_equal(expected, actual) 예상값과 실제 결과를 비교한다. 둘이 일치하지 않으면 테스트가 실패하며, 정확히 수정이 필요한 줄을 명확하게 안내한다.
3. 확장성을 위한 이름 규칙
일관성이 핵심이다. 개별 테스트 파일은 tc_ (테스트 케이스) 접두사를 사용하고, 모음이나 세트는 ts_ (테스트 세트) 접두사를 사용하여, 코드베이스가 커져도 탐색이 용이하도록 보장한다.
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
In the
Test::Unit framework, which naming convention is required for a method to be automatically executed as a test?The method must end with
_test.The method must be named
verify_logic.The method must start with
test_.The method must be inside a
setup block.✅ Correct!
Correct! Ruby's test runner looks for methods starting with 'test_' to identify test cases.❌ Incorrect
The test runner specifically identifies methods starting with the prefix 'test_'.QUESTION 2
What is the primary difference between a file named
tc_orders.rb and ts_app.rb?tc_ is for C++ code; ts_ is for Ruby.tc_ represents an individual Test Case; ts_ represents a Test Suite (collection).tc_ is used for local tests; ts_ is for server tests.There is no functional difference; it is purely aesthetic.
✅ Correct!
Exactly. 'tc' stands for Test Case, while 'ts' stands for Test Suite, which usually requires multiple test cases.❌ Incorrect
The prefix 'tc' is for individual files containing test cases, while 'ts' is for suites that group those files together.QUESTION 3
Why does
assert_equal take two arguments?To compare the 'expected' value with the 'actual' result.
To define the minimum and maximum execution time.
To specify the class name and the method name.
To allocate memory for the test results.
✅ Correct!
Yes. It uses both to provide detailed error messages (e.g., 'Expected A, but got B').❌ Incorrect
Assertions compare your intended outcome (expected) against the code's output (actual).QUESTION 4
What happens if a test fails in the middle of a suite?
The entire computer restarts.
Ruby skips the rest of the file and deletes the log.
The test runner reports the failure and continues to the next test method.
The Ruby interpreter enters an infinite loop.
✅ Correct!
Test runners are designed to report failures while still attempting to run all other tests for maximum visibility.❌ Incorrect
Failures are reported as they happen, but the runner continues to execute remaining tests.QUESTION 5
Which Ruby library must be required to use
Test::Unit::TestCase?require 'debug'require 'test/unit'require 'benchmark'require 'roman'✅ Correct!
Correct. test/unit is the standard library for unit testing in the Pragmatic Programmer's era of Ruby.❌ Incorrect
You must require 'test/unit' to access the TestCase class and assertion methods.Case Study: The 'Archaeology' of Fred's Bug
Applying Unit Testing Principles to Legacy Code
Fred is working on a complex financial system. He spends three days writing a large feature, only to find a bug on day four. He now has to search through 2,000 lines of code to find the error, a process he calls 'code archaeology.'
Q
1. If Fred had unit tested his code as he wrote it, what two specific benefits would he have gained regarding bug detection?
Solution:
First, the unit test would have caught the logic error immediately while the implementation was fresh in his mind. Second, since the test would have targeted only the handful of lines he had just written, he would have identified the bug instantly without having to search through the entire 2,000-line codebase.
First, the unit test would have caught the logic error immediately while the implementation was fresh in his mind. Second, since the test would have targeted only the handful of lines he had just written, he would have identified the bug instantly without having to search through the entire 2,000-line codebase.
Q
2. In the Roman numeral example provided, the second assertion failed. What does the error message tell the developer about the code's behavior?
Solution:
The error message identifies that the assertion expected 'ix' but the code actually returned 'iiiiiiiii'. This reveals that the logic is currently only capable of handling unit increments ('i') and fails to implement the subtraction or grouping logic required for symbols like 'x' or 'v'.
The error message identifies that the assertion expected 'ix' but the code actually returned 'iiiiiiiii'. This reveals that the logic is currently only capable of handling unit increments ('i') and fails to implement the subtraction or grouping logic required for symbols like 'x' or 'v'.